package com.feidee.data.report.util;

import com.feidee.data.email.util.ApplicationContextUtil;
import com.feidee.data.report.model.ReportConfiguration;
import com.feidee.data.report.util.mail.RMailSender;
import org.apache.log4j.Logger;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.PriorityBlockingQueue;

/**
 * Created by ZKJ on 2017/7/12.
 */
public class SendEmailThread implements Runnable {
    private ExecutorService reportFixedThreadPool;
    private PriorityBlockingQueue<ArrayList<ReportConfiguration>> hqlSqlQueue;
    private Map<String, Long> exceptionReport;
    private Map<String, Long> successReport;
    private DataSource confDataSource;
    private static Logger logger = Logger.getLogger(SendEmailThread.class);

    public SendEmailThread(ExecutorService reportFixedThreadPool, PriorityBlockingQueue<ArrayList<ReportConfiguration>> hqlSqlQueue, Map<String, Long> exceptionReport, Map<String, Long> successReport ) {
        this.reportFixedThreadPool = reportFixedThreadPool;
        this.hqlSqlQueue = hqlSqlQueue;
        this.exceptionReport = exceptionReport;
        this.successReport = successReport;
    }

    @Override
    public void run() {
        try {
            reportFixedThreadPool.shutdown();
            while(!reportFixedThreadPool.isTerminated()) {
                if (TimeUtil.getNow("HH:mm").equals("10:00") && !hqlSqlQueue.isEmpty()) {
                    List<String> successReportList = getSuccessReport();
                    RMailSender.sendReportExceptionMail(null, exceptionReport, successReport, successReportList, false, false);
                    logger.info("#### 十点定时发送预警邮件成功 ####");
                }
                Thread.sleep(59000);
            }
            List<String> successReportList = getSuccessReport();
            //发送报表预警邮件
            RMailSender.sendReportExceptionMail(null, exceptionReport, successReport, successReportList, true, false);
            //清除已发送报表信息
            exceptionReport.clear();
            successReport.clear();
            logger.info("#### 报表运行全部结束，发送预警邮件成功 ####");
        } catch (Exception e) {
            logger.info("#### 报表运行全部结束，发送预警邮件失败 ####");
        }
    }

    public List<String> getSuccessReport() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        ArrayList<String> successReport = new ArrayList<>();
        try {
            confDataSource = (DataSource) ApplicationContextUtil.getBean("confDataSource");
            connection = confDataSource.getConnection();
            String getTable = "SELECT report_key FROM report_exec_status WHERE exec_status=\"ZXCG\" AND SUBSTR(update_time,1,10)=\""
                    + TimeUtil.getNow("yyyy-MM-dd") + "\"";
            preparedStatement = connection.prepareStatement(getTable);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                String reportKey = resultSet.getString("report_key");
                successReport.add(reportKey);
            }
        } catch (Exception e) {
            logger.error("查询执行成功报表失败！", e);
        } finally {
            DbOperationUtil.cleanConnEnv(connection, preparedStatement, resultSet);
        }
        return successReport;
    }
}
